package com.hanry.listener;

import com.hanry.Main;
import com.hanry.command.CategoryBit;
import com.hanry.command.Command;
import com.hanry.command.CommandBit;
import com.hanry.component.FrontAndBackJoystickView;
import com.hanry.component.LeftAndRightJoystickView;
import com.hanry.component.OnLeftAndRightJoystickMoveListener;

public class LeftAndRightListener implements OnLeftAndRightJoystickMoveListener{
	private Main main = null;
	public LeftAndRightListener(Main main){
		this.main = main;
	}
	
	public void onValueChanged(int power, int direction) {
		Command command = null;
		if (power >= 3) {
			main.directionValueBit.setValueByte(power);
			//System.out.println("leftAndRightJoystick "+ power);
			if (LeftAndRightJoystickView.LEFT == direction) {
				if (main.lastFrontAndBackCommand == FrontAndBackJoystickView.FRONT) {
					command = new Command(new CategoryBit(
							CategoryBit.DIRECTION),
							new CommandBit(
									CommandBit.LEFT_FORWARD),
							main.directionValueBit);
				} else if (main.lastFrontAndBackCommand == FrontAndBackJoystickView.BACK) {
					command = new Command(
							new CategoryBit(
									CategoryBit.DIRECTION),
							new CommandBit(CommandBit.LEFT_BACK),
							main.directionValueBit);
				} else {
					command = new Command(new CategoryBit(
							CategoryBit.DIRECTION),
							new CommandBit(CommandBit.LEFT),
							main.directionValueBit);
				}
				main.sendCommand(command);
				main.lastLeftAndRightCommand = LeftAndRightJoystickView.LEFT;
			} else if (LeftAndRightJoystickView.RIGHT == direction) {
				if (main.lastFrontAndBackCommand == FrontAndBackJoystickView.FRONT) {
					command = new Command(new CategoryBit(
							CategoryBit.DIRECTION),
							new CommandBit(
									CommandBit.RIGHT_FORWARD),
							main.directionValueBit);
				} else if (main.lastFrontAndBackCommand == FrontAndBackJoystickView.BACK) {
					command = new Command(new CategoryBit(
							CategoryBit.DIRECTION),
							new CommandBit(
									CommandBit.RIGHT_BACK),
							main.directionValueBit);
				} else {
					command = new Command(new CategoryBit(
							CategoryBit.DIRECTION),
							new CommandBit(CommandBit.RIGHT),
							main.directionValueBit);
				}
				main.sendCommand(command);
				main.lastLeftAndRightCommand = LeftAndRightJoystickView.RIGHT;
			}
		}
	}

	public void OnReleased() {
	}

	public void OnReturnedToCenter() {
		Command command = null;
		if (main.lastFrontAndBackCommand == FrontAndBackJoystickView.FRONT) {
			command = new Command(new CategoryBit(
					CategoryBit.DIRECTION), new CommandBit(
					CommandBit.FORWARD), main.directionValueBit);
		} else if (main.lastFrontAndBackCommand == FrontAndBackJoystickView.BACK) {
			command = new Command(new CategoryBit(
					CategoryBit.DIRECTION), new CommandBit(
					CommandBit.BACK), main.directionValueBit);
		} else {
			command = new Command(new CategoryBit(
					CategoryBit.DIRECTION), new CommandBit(
					CommandBit.STOP), main.directionValueBit);
		}

		main.sendCommand(command);
		main.lastLeftAndRightCommand = LeftAndRightJoystickView.ORIGIN;
	}
}
